<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true">
      <el-form-item style="width: 150px">
        <el-input v-model="searchMap.data.Merchantname" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item style="width: 150px">
        <el-input
          v-model="searchMap.data.phone"
          placeholder="手机号"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="searchBtn()"
          >查询</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 商户信息列表表格 -->
    <el-table :data="MerchantLsit" style="width: 100%" ref="MerchantTable">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="username" label="姓名"></el-table-column>
      <el-table-column prop="status" label="状态"> 
        <template slot-scope="scope">
          <div>
            {{scope.row.status | statusFormat}}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="tel" label="手机号码"></el-table-column>
    
      <el-table-column label="操作" width="260">
        <template slot-scope="scope">
          <!-- 自定义展示 -->
          <div>
            <el-button @click="showInfo(scope.row)" type="primary" size="small">查看</el-button>
            <!-- <el-tooltip content="修改商户信息" placement="top" :enterable="false">
              <el-button type="warning" size="mini" icon="el-icon-edit" circle :value="scope.row" @click="updateMerchant(scope.row)"></el-button>
            </el-tooltip> -->
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页栏 -->
    <el-pagination
      style="text-align: center"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="searchMap.page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 商户信息弹出层 编号2-->
    <el-dialog
        title="商户信息"
        :visible.sync="dialogFormVisible2"
        :close-on-click-modal="false"
        >
        <el-form :model="MerchantEntity"  ref="MerchantForm" :rules="MerchantRules">
            <el-form-item label="姓名" label-width="100px" prop="name">
            <el-input v-model="MerchantEntity.username"  style="color:black" placeholder="商户姓名 必填！！"></el-input>
            </el-form-item>
            <el-form-item label="手机号码"  label-width="100px" prop="phone">
            <el-input v-model="MerchantEntity.tel" style="color:black" placeholder="商户手机号码 必填！！"></el-input>
            </el-form-item>
        </el-form>
        </el-dialog>


  </div>
</template>


<script>
import { listSearch,MerchantSave,delById,delByIds,resumeOrCancel } from "@/api/merchant.js";
import {isvalidPhone} from '@/api/validate.js'
 var validPhone=(rule, value,callback)=>{
      if (!value){
          callback(new Error('请输入电话号码'))
      }else  if (!isvalidPhone(value)){
        callback(new Error('请输入正确的11位手机号码'))
      }else {
          callback()
      }
  }
export default {
  created() {
    this.search();
  },
  data() {
    return {
      searchMap: {
        page: 1,
        limit: 10,
        data: {},
      },
      MerchantLsit: [],
      dialogFormVisible2:false,
      total: 0,
      //商户表单校验
      MerchantRules: {
        name:[
          { required: true, message: '请输入商户姓名', trigger: 'blur' },
        ],
        phone:[
          { required: true, message: '请输入商户电话号码', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validPhone }
        ]
      },
      //商户配镜信息表单校验
      MerchantinfoRules: {
        price:[
          { pattern: /^-?\d+\.?\d*$/, message: '请输入纯数字', trigger: 'blur'}
        ]
      },
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      MerchantInfoList:[],
      priceTotal:0,
      MerchantinfoEntity: {},
      MerchantEntity:{},
      MerchantId:0,
    };
  },
  methods: {
    showInfo(val){
 this.dialogFormVisible2 = true;
      this.MerchantEntity =val;
    },
    searchBtn(){
      this.searchMap.page=1;
      this.search();
    },
    search() {
      listSearch(this.searchMap).then((res) => {
        this.MerchantLsit = res.data;
        this.total = res.total;
      });
    },
    handleSizeChange(pageSize) {
      //改变每页数据显示条数触发
      this.searchMap.limit = pageSize;
      this.search();
    },
    handleCurrentChange(currentPage) {
      //查看指定页数事件触发
      this.searchMap.page = currentPage;
      this.search();
    },
  

    //修改商户信息按钮触发事件
    updateMerchant(row){
      this.dialogFormVisible2 = true;
      this.MerchantEntity = JSON.parse(JSON.stringify(row));
    },
    //添加保存与修改保存
    saveMerchant(){
      MerchantSave(this.MerchantEntity).then(res=>{
        if(res.success){
          this.dialogFormVisible2 = false;
          this.$message({
            message: res.msg,
            type: "success",
            center: true,
          });
          this.search();
        }else{
          this.$message({
            message: res.msg,
            type: "error",
            center: true,
          });
        }
      });
    },

    //商户信息行删除
    resumeOrCancelStatus(row){
      this.$confirm("此操作将修改用户状态, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          resumeOrCancel(row).then((res) => {
            if (res.success) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.search();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //商户信息批量删除
    delMerchants(){
      var rowDatas = this.$refs.MerchantTable.selection;
      var ids=[];
      
      this.$confirm("此操作将删除选中的多条记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          rowDatas.forEach(element => {
             ids.push(element.id);
          });
          delByIds(ids).then((res) => {
            if (res.success) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.search();
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //商户信息表单submit按钮
    submitMerchantForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.saveMerchant();
          } else {
            this.$message({
                type: "error",
                message: "请按要求填写商户信息！！",
              });
            return false;
          }
        });
    },
    //商户信息表单submit按钮
    submitMerchantinfoForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.saveMerchantInfo();
          } else {
            this.$message({
                type: "error",
                message: "请按要求填写商户配镜信息！！",
              });
            return false;
          }
        });
    },
    //商户配镜信息添加
    addMerchantInfo(row){
      this.MerchantId = row.id;
      this.dialogFormVisible3 = true;
      this.MerchantinfoEntity = {};
    },
    saveMerchantInfo(){
      console.log(this.MerchantId)
      console.log(this.MerchantinfoEntity);
      this.MerchantinfoEntity.MerchantId=this.MerchantId;
      MerchantInfoSave(this.MerchantinfoEntity).then(res=>{
        if(res.success){
          this.dialogFormVisible3 = false;
          this.dialogFormVisible1 = false;
          this.$message({
            message: res.msg,
            type: "success",
            center: true,
          });
          this.search();
        }else{
          this.$message({
            message: res.msg,
            type: "error",
            center: true,
          });
        }
      });
    },
    //修改商户配镜信息
    updateMerchantInfo(row){
      this.dialogFormVisible3 = true;
      this.MerchantinfoEntity = JSON.parse(JSON.stringify(row));
    },
    //删除商户配镜信息
    delMerchantInfo(row){
      this.$confirm("此操作将删除该条记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((res) => {
          MerchantInfoDelById(row.id).then((res) => {
            if (res.success) {
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.dialogFormVisible1=false;
              this.dialogFormVisible3=false;
            } else {
              this.$message({
                type: "error",
                message: res.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    formatTime(row, column) {
      const updateTime = new Date(row[column.property]);
      return (
        updateTime.getFullYear() +
        "年" +
        updateTime.getMonth() +
        "月" +
        updateTime.getDate() +
        "日 " +
        updateTime.getHours() +
        ":" +
        updateTime.getMinutes()
      );
    },
  },
  filters:{
    statusFormat(source){
        if(source == 1){
          return "正常"
        }
        if(source == 2){
          return "注销"
        }
        if(source == 3){
          return "待激活"
        }
    }
  }
};
</script>
<style lang="scss">
.goodsStatus {
  background-color: rgb(51, 171, 160);
}
</style>